package com.rz.paas.test.viewPager

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.widget.RelativeLayout
import com.rz.paas.test.R

class BannerView(context: Context, attributeSet: AttributeSet): RelativeLayout(
    context,
    attributeSet
) {

    private val mBannerVp: BannerViewPager by lazy {
        findViewById(R.id.banner_vp)
    }

    private var mAdapter: BannerAdapter? = null

    private val mContext: Context? = null

    private val mCurrentPosition = 0

    init {
        inflate(context, R.layout.ui_banner_layout, this)
    }

    private val TAG = "BannerView"
    fun setAdapter(adapter: BannerAdapter) {
        mAdapter = adapter
        mBannerVp.setAdapter(adapter)
        mBannerVp.addOnPageChangeListener(object : SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                Log.d(TAG, "onPageSelected:position:$position ")
                adapter.onPageChanged(position%adapter.getCount())
            }
        })



        post {
            val width = measuredWidth
            val height = measuredHeight
            layoutParams.height = height
            mBannerVp.getLayoutParams().height = height
        }
    }

    fun setPageIndex(index:Int){
        mBannerVp.currentItem = index
    }
}